﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Transactions;

namespace Web.Areas.Admin.Controllers
{
    public class ManagerController : Controller
    {
        private readonly Manager.BLL.Admin.M_AdminBll bll = new Manager.BLL.Admin.M_AdminBll();

        //
        // GET: /Admin/Manager/
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ManagerSet, Ope = "view")]
        public ActionResult Index()
        {
            var query = from m in bll.GetModelList("")
                        select new Models.Manager
                        {
                            UserId=m.UserId,
                            Username=m.Username,
                            RealName=m.Realname,
                            Email=m.Email,
                            RoleName=new Manager.BLL.Admin.M_AdminRoleBll().GetModel(m.RoleID.Value).RoleName
                        };
            return View(query.ToList<Models.Manager>());
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ManagerSet, Ope = "add")]
        public ActionResult Add()
        {
            BindSelect();
            return View();
        }

        /// <summary>
        /// 角色下拉列表框
        /// </summary>
        public void BindSelect()
        {
            Manager.BLL.Admin.M_AdminRoleBll roleBll = new Manager.BLL.Admin.M_AdminRoleBll();
            IEnumerable<SelectListItem> roles = from r in roleBll.GetModelList("") select new SelectListItem { Value = r.RoleID.ToString(), Text = r.RoleName };
            ViewBag.RoleSelect = roles;
        }

        /// <summary>
        /// 表单提交添加管理员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ManagerSet, Ope = "add")]
        [HttpPost]
        public ActionResult Add(Manager.Model.Admin.M_Admin model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    model.Password = Manager.Tools.DEncrypt.DESEncrypt.Encrypt(model.Password);
                    int r = bll.Add(model);
                    if (r > 0)
                        return RedirectToAction("Index", "Manager");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ManagerSet, Ope = "edit")]
        public ActionResult Edit(int id)
        {
            if (!bll.Exists(id))
                return RedirectToAction("Index");
            BindSelect();
            return View(bll.GetModel(id));
        }

        /// <summary>
        /// 提交表单编辑管理员
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ManagerSet, Ope = "edit")]
        public ActionResult Edit(int id,Manager.Model.Admin.M_Admin model)
        {
            if (id == 1)
                ModelState.AddModelError("", Resources.Global.msg_operate_alert);
            try
            {
                if (ModelState.IsValid)
                {
                    model.UserId = id;
                    model.Password = Manager.Tools.DEncrypt.DESEncrypt.Encrypt(model.Password);
                    bool r = bll.Update(model);
                    if (r)
                        return RedirectToAction("Index", "Manager");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ManagerSet, Ope = "del")]
        public ActionResult Del(int id)
        {
            if (id == 1)
                return Content("<script>alert('" + Resources.Global.msg_operate_alert + "');location.href='/Admin/Manager/Index';</script>");

            if(!bll.Exists(id))
                return Content("<script>alert('" + Resources.Global.msg_select_exist + "');location.href='/Admin/Manager/Index';</script>");

            try
            {
                bool r = bll.Delete(id);
                if (r)
                    return Content("<script>alert('" + Resources.Global.msg_success + "');location.href='/Admin/Manager/Index';</script>");
                else
                    return Content("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/Manager/Index';</script>");
                    
            }
            catch (Exception)
            {
                return JavaScript("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/Manager/Index';</script>");
            }
        }

        
    }
}
